package com.liu.es.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;

import java.io.IOException;
import java.util.Random;

public class ES批量操作 {
    public static String[] generatorArray() {
        String[] name = {"zhangsan", "lisi", "wangwu", "zhaoliu", "xiongda", "xionger"};
        return name;
    }

    public static void main(String[] args) throws IOException {
        BulkRequest bulk = new BulkRequest();
        RestHighLevelClient rest = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
        ObjectMapper om = new ObjectMapper();
        //批量添加
//        String[] names = generatorArray();
//        User u = null;
//        IndexRequest index=null;
//        IndexResponse i=null;
//        for (int j=0;j<names.length;j++){
//
//            index = new IndexRequest().index("user").id(Integer.toString(j));
//            u = new User(names[j], j % 2 == 0 ? "famale" : "male", j);
//            String json = om.writeValueAsString(u);
//            index.source(json, XContentType.JSON);
//            bulk.add(index);
//        }

        //批量删除

//        for (int j=0;j<names.length;j++){
//            bulk.add(new DeleteRequest().index("user").id(Integer.toString(j)));
//        }
        BulkResponse res = rest.bulk(bulk, RequestOptions.DEFAULT);
        System.out.println(res.status());


    }

    @Test
    public void opera_Bulk() throws IOException {
        RestHighLevelClient rest = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));
        //创建Bulk对象
        BulkRequest bulk = new BulkRequest();
        IndexRequest index=null;
        DeleteRequest delete=null;
        String[] name= {"zhangsan", "lisi", "wangwu", "zhaoliu", "xiongda", "xionger"};
        ObjectMapper om = new ObjectMapper();
        User u=null;
        String json="";

        for (int i=0;i<name.length;i++){
            u=new User(name[i],i%2==0?"男":"女",i%2==0?i:i%3);
            json=om.writeValueAsString(u);
            index=new IndexRequest();
            index.index("user").id(Integer.toString(i));
            index.source(json,XContentType.JSON);
            bulk.add(index);
        }



//        for (int i=0;i<name.length;i++){
//            delete=new DeleteRequest();
//            delete.index("user").id(Integer.toString(i));
//            bulk.add(delete);
//        }
        BulkResponse bulk1 = rest.bulk(bulk, RequestOptions.DEFAULT);



        rest.close();


    }
}
